package com.xying.controller;

import com.xying.common.ResponseCode;
import com.xying.common.ServerResponse;
import com.xying.pojo.Usr;
import com.xying.service.UsrService;

import com.xying.util.ValidateUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Hisen
 * @date 2017/11/6
 */
@Controller
@RequestMapping("/usr")
public class UsrController {

  @Autowired
  private UsrService usrService;

  /**
   * 用户登录
   * @param request
   * @param username
   * @param password
   * @return
   */
  @ResponseBody
  @RequestMapping(value = "login.do", method = RequestMethod.POST)
  public ServerResponse login(HttpServletRequest request, HttpServletResponse response, String username, String password){
    ServerResponse<Usr> serverResponse = usrService.login(username, password);
    if(!serverResponse.isSuccess()) {
      return serverResponse;
    }

    //中间数据
    HttpSession session = request.getSession();
    Usr usr = serverResponse.getData();

    //存入session中
    session.setAttribute(session.getId(), usr);

    return serverResponse;
  }

  /**
   * 用户退出
   * @param request
   * @return
   */
  @ResponseBody
  @RequestMapping(value = "logout.do", method = RequestMethod.POST)
  public ServerResponse logout(HttpServletRequest request){
    request.getSession().removeAttribute(request.getSession().getId());
    return ServerResponse.createBySuccess();
  }

  /**
   * 用户名检测
   * @param request
   * @param username
   * @return
   */
  @ResponseBody
  @RequestMapping(value = "checkUsername.do")
  public ServerResponse checkUsername(HttpServletRequest request, String username){
    ServerResponse serverResponse = ValidateUtil.validate(username);
    if (!serverResponse.isSuccess()){
      return serverResponse;
    }

    serverResponse = usrService.checkUsername(username);

    return serverResponse;
  }

  /**
   * 用户注册(测试使用)
   * @param usr
   * @return
   */
  @ResponseBody
  @RequestMapping(value = "register.do", method = RequestMethod.POST)
  public ServerResponse register(Usr usr){
    ServerResponse serverResponse = ValidateUtil.validate(usr.getUserName(), usr.getPassword(), usr.getSex(), usr.getDepartment());
    if (!serverResponse.isSuccess()){
      return serverResponse;
    }

    return usrService.register(usr);
  }


  /**
   * 获取用户信息
   * @param request
   * @return
   */
  @ResponseBody
  @RequestMapping("getPersonalInfo.do")
  public ServerResponse getPersonalInfo(HttpServletRequest request){
    ServerResponse<Usr> serverResponse = ValidateUtil.validateNormalLogin(request);
    if (!serverResponse.isSuccess()){
      return serverResponse;
    }

    return usrService.getPersonalInfo(serverResponse.getData());
  }

  /**
   * 保存用户修改的信息
   * @param request
   * @param sex
   * @return
   */
  @ResponseBody
  @RequestMapping("save.do")
  public ServerResponse save(HttpServletRequest request, String idNo, Integer sex, String department){
    ServerResponse serverResponse = ValidateUtil.validate(sex, department);
    if(!serverResponse.isSuccess()) {
      return serverResponse;
    }

    serverResponse = ValidateUtil.validateNormalLogin(request);
    if (!serverResponse.isSuccess()){
      return serverResponse;
    }

    Usr usr = (Usr) serverResponse.getData();
    if(!usr.getIdNo().equals(idNo)){
      return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), ResponseCode.NEED_LOGIN.getDesc());
    }

    usr.setSex(sex);
    usr.setDepartment(department);

    //更新session
    request.getSession().setAttribute(request.getSession().getId(), usr);

    return usrService.savePersonalInfo(usr);
  }

  /**
   * 用户登录状态修改密码
   * @param request
   * @param idNo
   * @param oldPassword
   * @param newPassword
   * @return
   */
  @ResponseBody
  @RequestMapping("updatePassword.do")
  public ServerResponse updatePassword(HttpServletRequest request, String idNo, String oldPassword, String newPassword) {
    ServerResponse<Usr> serverResponse = ValidateUtil.validateNormalLogin(request);
    if (!serverResponse.isSuccess()){
      return serverResponse;
    }

    Usr usr = serverResponse.getData();
    if(!usr.getIdNo().equals(idNo)){
      return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(), ResponseCode.PERMISSION_DENIED.getDesc());
    }

    return usrService.updatePassword(idNo, oldPassword, newPassword);
  }

}
